<?php 

class mission_ctr extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
      
		  $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
		$this->load->library('Convert_ctr');// elle convert les chiffres en lettre arab
    }
	  
	 function index()
	{
		
		 $this->load->library('session');
		
		$this->load->model('mission_mdl'); // load the file welcome_mdl.php
		
	    $config = array();
          $config["base_url"] = base_url( "bc_ctr/index");
		  $config["total_rows"] =$this->mission_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->mission_mdl->get_all($config["per_page"], $page);
	   
	   
        $data["links"] = $this->pagination->create_links();
		$data['titre']='قائمة المهمات ';
		  $this->load->view('mission_vew',$data);  // send the result from the model to the view 
		  
	
	
	}
	
		public function ajout_mission()
    {
		$nom= $this->input->post('nom');
		$grade= $this->input->post('grade');
		$date= $this->input->post('date');
		$transport= $this->input->post('transport');
		$normale= $this->input->post('normale');
		$nuit= $this->input->post('nuit');
		$numero= $this->input->post('numero');
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('mission_mdl'); // load 
		
		$erreurs=NULL;
		if(!$nom)$erreurs['nom']=TRUE;
		if(!$grade)$erreurs['grade']=TRUE;
		
		if($numero){
			$nu=$this->mission_mdl->existe_numero($numero);
			if($nu){
				$erreurs['numero']=TRUE;
			}
		}else{
			
		$t_numero=$this->mission_mdl->get_numero();
		if($t_numero){
			$numero=$t_numero[0]->numero+1;
		}else $numero=1;
		}
		
		if($erreurs) {
			$msg=$erreurs;
			$this->ajout($msg);
			}
		else {
		$montant=0;
		if(!$normale)$normale=0;
		if(!$nuit)$nuit=0;
		if(!$transport)$transport=0;
		if($grade<11){
			$recup_normale=250*$normale;
			$recup_nuit=1200*$nuit;
		}else{
			$recup_normale=800*$normale;
			$recup_nuit=2000*$nuit;
		}
		$montant=$recup_normale+$recup_nuit+$transport;
		if(!$date){
			
		$today = getdate();
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		}
			
		$this->mission_mdl->ajout_mission($nom,$numero,$grade,$date,$transport,$normale,$nuit,$montant);
		
		$this->index(); 
		}
    }

      	public function modifier_mission($id_mission)
    {
		$nom= $this->input->post('nom');
		$grade= $this->input->post('grade');
		$date= $this->input->post('date');
		$transport= $this->input->post('transport');
		$normale= $this->input->post('normale');
		$nuit= $this->input->post('nuit');
		$numero= $this->input->post('numero');
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('mission_mdl'); // load 
		$info=$this->mission_mdl->get_detaille($id_mission);
		
		$erreurs=NULL;
		if(!$nom)$erreurs['nom']=TRUE;
		if(!$grade)$erreurs['grade']=TRUE;
		
		if($numero ){
			if($info[0]->numero!=$numero){
				
			$nu=$this->mission_mdl->existe_numero($numero);
			if($nu){
				$erreurs['numero']=TRUE;
			}
			}
		}else{
			
		$t_numero=$this->mission_mdl->get_numero();
		if($t_numero){
			$numero=$t_numero[0]->numero+1;
		}else $numero=1;
		}
		
		if($erreurs) {
			$msg=$erreurs;
			$this->ajout($msg);
			}
		else {
		$montant=0;
		if(!$normale)$normale=0;
		if(!$nuit)$nuit=0;
		if(!$transport)$transport=0;
		if($grade<11){
			$recup_normale=250*$normale;
			$recup_nuit=1200*$nuit;
		}else{
			$recup_normale=800*$normale;
			$recup_nuit=2000*$nuit;
		}
		$montant=$recup_normale+$recup_nuit+$transport;
		if(!$date){
			
		$today = getdate();
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		}
			
		$this->mission_mdl->update_mission($id_mission,$nom,$numero,$grade,$date,$transport,$normale,$nuit,$montant);
		
		$this->index(); 
		}
    }

          	public function engager($id_mission)
    {
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('mission_mdl'); // load 
		$info=$this->mission_mdl->get_detaille($id_mission);
		
		$id_article=1;
			$num_enga=$this->engagement_mdl->get_engagement_prec($id_article);
		
		if(!$num_enga){
			 echo "<script> alert(\" non تمت عملية الالتزام بنجاح\")</script>";
			$this->detaille_mission($id_mision);
		}else{
			if($num_enga[0]->nature==1){// si nature est depense
				$montant_prec=$num_enga[0]->montant_precedent-$num_enga[0]->montant_engagement;
			}else{
				$montant_prec=$num_enga[0]->montant_engagement+$num_enga[0]->montant_precedent;
			}
			
			
			$montant=$info[0]->montant;
			$numero=$num_enga[0]->numero+1;
			$nature=1;
			$today = getdate();
			$time=$today[0];
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			$this->engagement_mdl->ajout_engagement3($id_article,$nature,$date,$numero,$time,$montant,$montant_prec);
			$num_enga=$this->engagement_mdl->get_engagement_numero($id_article);
			$id_engagement=$num_enga[0]->id_engagement;
			
		
		$this->mission_mdl->engager_mission($id_mission,$id_engagement);

		//$this->mission_mdl->update_mission($id_mission,$nom,$numero,$grade,$date,$transport,$normale,$nuit,$montant);
		
		$this->index(); 
		}
    }
	
		
			public function ajout($err)
    {
		$result['err']=$err;
		$this->load->view('mission_ajout_vew',$result); 
    }
	
				public function detaille_mission($id_mision)
    {
		$this->load->model('mission_mdl'); // load 
		$info=$this->mission_mdl->get_detaille($id_mision);
		$result['result']=$info;
		$this->load->view('mission_detaille_vew',$result); 
    }

   				public function imprimer($id_mision)
    {
		$this->load->model('mission_mdl'); // load 
		$info=$this->mission_mdl->get_detaille($id_mision);
		$result['result']=$info;
		$this->load->view('mission_imprimer_vew',$result); 
    }
	
	

}
